import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store'

const routes = [
    {
        path:'/',
        component:()=>import('../views/Main.vue'),
        redirect: '/home',
        children:[
            {
                path:'home',
                component:()=>import('../views/Home/index.vue'),
                meta:{
                    label:'首页',
                    Icon:'HomeFilled',
                    roles:'*'
                },
            },
            {
                path:'mall',
                component:()=>import('../views/Mall/index.vue'),
                meta:{
                    label:'商品管理',
                    Icon:'GoodsFilled',
                    roles:['admin','editor']
                },
            },
            {
                path:'user',
                component:()=>import('../views/User/index.vue'),
                redirect: '/user/edit',
                meta:{
                    label:'用户管理',
                    Icon:'UserFilled',
                    roles:['admin']
                },
                children:[
                    {
                        path:'edit',
                        component:()=>import('../views/User/UserEdit/index.vue'),
                        meta:{
                            label:'用户编辑',
                            Icon:'EditPen',
                            roles:['admin']
                        },
                    },
                    {
                        path:'loa',
                        component:()=>import('../views/User/UserRights/index.vue'),
                        meta:{
                            label:'用户权限',
                            Icon:'Avatar',
                            roles:['admin']
                        },
                    },
                ],
            },
            {
                path:'page',
                component:()=>import('../views/Page/index.vue'),
                redirect: '/page/page1',
                meta:{
                    label:'其他',
                    Icon:'MessageBox',
                    roles:['admin','editor']
                },
                children:[
                    {
                        path:'page1',
                        component:()=>import('../views/Page/Page1/index.vue'),
                        meta:{
                            label:'页面1',
                            Icon:'MessageBox',
                            roles:['admin','editor']
                        },
                    },
                    {
                        path:'page2',
                        component:()=>import('../views/Page/Page2/index.vue'),
                        meta:{
                            label:'页面2',
                            Icon:'MessageBox',
                            roles:['admin','editor']
                        },
                    },
                ],
            },
        ],
    },
    {
        path:'/login',
        component:()=>import('../views/Login/index.vue'),
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

//全局前置守卫，权限校验
router.beforeEach((to,from,next)=>{
    if(to.path === '/login') return next()
    if(store.state.user.islogin){
        const Grade = store.state.user.userifo.Grade
        const GradeConfig = to.meta.roles
        if(!GradeConfig) return
        if(!(GradeConfig !== '*' && typeof GradeConfig.length === 'number' && GradeConfig.indexOf(Grade) === -1)){
            next()
        }
    }else{
        next('/login')
    }
})

//全局后置守卫，用于更新VueX的当前路由路径对象和路由路径对象列表
router.afterEach((to,from)=>{
    if(to.path !== '/login'){
        const obj = {
            label: to.meta.label,
            path: to.path,
        }
        store.commit('Tag/ADDROUTERPAHT', obj)
    }
})

export default router